package com.chen10.cattle.controller;

import com.chen10.cattle.common.exception.BusinessException;
import com.chen10.cattle.common.protocal.ResponseResult;
import com.chen10.cattle.common.protocal.ResponseVO;
import com.chen10.cattle.model.pojo.OtherFinanceRecord;
import com.chen10.cattle.service.FinanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 财务管理相关控制器
 */
@RestController
@RequestMapping("/api/finance")
@Slf4j
public class FinanceController {
    @Resource
    private FinanceService financeService;

    /**
     * 获取所有其他财务记录
     */
    @GetMapping("/records")
    public ResponseVO<List<OtherFinanceRecord>> getOtherFinanceRecords() {
        log.info("获取所有其他财务记录");
        try {
            List<OtherFinanceRecord> records = financeService.getAllOtherFinanceRecords();
            return ResponseResult.success(records);
        } catch (BusinessException e) {
            return ResponseResult.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取其他财务记录失败", e);
            return ResponseResult.error("获取其他财务记录失败");
        }
    }

    /**
     * 添加其他财务记录
     */
    @PostMapping("/add")
    public ResponseVO<OtherFinanceRecord> addOtherFinanceRecord(@RequestBody OtherFinanceRecord record) {
        log.info("添加其他财务记录: {}", record);
        try {
            OtherFinanceRecord savedRecord = financeService.addOtherFinanceRecord(record);
            return ResponseResult.success(savedRecord);
        } catch (BusinessException e) {
            return ResponseResult.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("添加其他财务记录失败", e);
            return ResponseResult.error("添加其他财务记录失败");
        }
    }

    /**
     * 删除其他财务记录
     */
    @DeleteMapping("/delete/{id}")
    public ResponseVO<Boolean> deleteOtherFinanceRecord(@PathVariable Integer id) {
        log.info("删除其他财务记录, ID: {}", id);
        try {
            boolean result = financeService.deleteOtherFinanceRecord(id);
            return ResponseResult.success(result);
        } catch (BusinessException e) {
            return ResponseResult.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("删除其他财务记录失败", e);
            return ResponseResult.error("删除其他财务记录失败");
        }
    }
}